/// pages/fabu/fabu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeId: "",
    title: "",
    content: "",
    imgSrc: "/pages/image/upload.png",
    select: false,
    slect_tip: '选择新闻类别->',
    selects: ['1', '2', '3'],
    typeId: '',
    down:"/pages/image/change1.png",
    p:2
  },
  bindShowMsg() {
    if(this.data.p==2){
      this.setData({
        select: !this.data.select,
        down:"/pages/image/change2.png",
        p:1
      })
    }else{
      if(this.data.p==1){
        this.setData({
          select: !this.data.select,
          down:"/pages/image/change1.png",
          p:2
        })
      }
    }
  },
  mySelect(e) {
    var that = this
    // console.log(e)
    let type = e.currentTarget.dataset.name
    // console.log(that.data.selects.indexOf(type))
    var name = e.currentTarget.dataset.name
    this.setData({
      slect_tip: name,
      typeId: that.data.selects.indexOf(type),
      select: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this; //this在函数内部指向变化,所以需要将全局this指向保存到that中
    wx.request({
      url: 'https://www.yangtukang.xyz/wxnews/newsType/getList', //需更换需请求数据的接口
      method: 'GET', //请求方式 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // console.log(res.data.data);
        // console.log(res.data.data.length);
        let types = []
        for (let i = 0; i < res.data.data.length; i++) {
          types[i] = res.data.data[i].name
        }
        // console.log(types)
        that.setData({
          selects: types
        })
      },
      fail: function (err) {}, //请求失败
    })
  },
  upLoadImg() {
    var that = this
    // console.log("图片上传")
    wx.chooseMedia({
      // url:"https://www.yangtukang.xyz/static/images",
      success: function (res) {
        // console.log(res)
        let img = res.tempFiles[0].tempFilePath
        // that.setData({
        //   imgSrc: res.tempFiles[0]
        // })
        wx.uploadFile({
          filePath: img,
          name: 'file',
          method: "POST",
          url: 'https://www.yangtukang.xyz/api/upload/images',
          success: function (res) {
            let data = JSON.parse(res.data)
            that.setData({
              imgSrc: data.data.src
            })
          }
        })
      }
    })
  },
  publish() {
    let typeId = this.data.typeId
    let title = this.data.title
    let content = this.data.content
    let imgSrc = this.data.imgSrc

    // console.log("发布新闻")
    // console.log("新闻标题: " + title)
    // console.log("新闻内容: " + content)
    // console.log("新闻图片: " + imgSrc)
    // console.log("新闻类别: " + typeId)

      wx.request({
        url: 'https://www.yangtukang.xyz/wxnews/news/save', //需更换需请求数据的接口
        method: 'POST', //请求方式 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          "typeId": typeId,
          "imgSrc": imgSrc,
          "title": title,
          "content": content
        },
        success: function (res) {
          console.log(res.data.message)
        },
        fail: function (err) {}, //请求失败
      })
  },
  reset() {
    var that = this
    that.setData({
      type: "",
      title: "",
      content: "",
      imgSrc: "/pages/image/upload.png"
    })
  }
})